import { fakeChartData, getAllCount, getMemberTrend,getScrollInfo } from './service';

const initState = {
  visitData: [],
  visitData2: [],
  salesData: [],
  searchData: [],
  offlineData: [],
  offlineChartData: [],
  salesTypeData: [],
  salesTypeDataOnline: [],
  salesTypeDataOffline: [],
  radarData: [],
  allcount: {},
  newMemberData: [],
  expireMemberData: [],
  scrollInfo:{}
};
const Model = {
  namespace: 'dashboardAndanalysis',
  state: initState,
  effects: {
    *fetch(_, { call, put }) {
      // const response = yield call(fakeChartData);
      // yield put({
      //   type: 'save',
      //   payload: response,
      // });
    },

    *fetchSalesData(_, { call, put }) {
      const response = yield call(fakeChartData);
      yield put({
        type: 'save',
        payload: {
          salesData: response.salesData,
        },
      });
    },
    *getAllCount({ payload }, { call, put }) {
      const result = yield call(getAllCount, payload);
      if (result.code == 0) {
        yield put({
          type: 'common',
          payload: {
            allcount: result.data
          },
        });
      }
    },
    *getMemberTrend({ payload }, { call, put }) {
      const result = yield call(getMemberTrend, payload);
      if (result.code == 0) {
        if (Array.isArray(result.data)) {
          yield put({
            type: 'common',
            payload: {
              newMemberData: result.data[0].data,
              expireMemberData: result.data[1].data
            },
          });
        }
      }
    },
    *fetchScrollInfo({payload}, { call, put }) {
      const response = yield call(getScrollInfo,payload);
      yield put({
        type: 'save',
        payload: {
          scrollInfo: response,
        },
      });
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    clear() {
      return initState;
    },
  },
};
export default Model;
